"use strict";

import Vue from 'vue';
import { MessageBox, Message } from 'element-ui'
import axios from "axios";
import { getToken, removeItem, removeToken } from '@/plugins/auth'

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.header.code !== 0 && res.header.code !== 0x3FFF0005) {
      Message({
        message: res.body.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      // 未登录
      if (res.header.code === 10000 ) {
        setTimeout(function() {
          removeToken()
          removeItem('baseParam')
          window.location.href = '/#/login'
        }, 1000)
        return
      }
      return Promise.reject(res.body.msg)
    } else {
      return res
    }
  },
  function(error) {

    switch (error.response.data.status) {
      case 404:
        Message({
          message: '接口不存在：' + error.response.data.path || error.response.data.error || '页面不存在',
          type: 'error',
          duration: 5 * 1000
        })
        break
      case 403:
        Message({
          message: error.response.data.message || error.response.data.error || '无权访问',
          type: 'error',
          duration: 5 * 1000
        })
        break
      case 503:
        Message({
          message: error.response.data.message || error.response.data.error || '网关错误',
          type: 'error',
          duration: 5 * 1000
        })
        break
      case 500:
        Message({
          message: error.response.data.message || error.response.data.error || '系统错误',
          type: 'error',
          duration: 10 * 1000
        })
        break
      default:
        Message({
          message: error.response.data.message || error.response.data.error || '系统错误',
          type: 'error',
          duration: 10 * 1000
        })
    }
    return Promise.reject(error);
  }
);

Plugin.install = function(Vue, options) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
